﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Concurrent;

namespace HttpCrawler.Extensions.Tests
{
    [TestClass]
    public class ParallelHelperTests
    {
        [TestMethod]
        public void While_Processes_Expected_Work_Load()
        {
            var values = Enumerable.Range(1, 50);
            var inputQueue = new ConcurrentQueue<int>(values);
            var outputQueue = new ConcurrentQueue<int>();

            ParallelHelper.While(
                () => !inputQueue.IsEmpty,
                () =>
                {
                    int result;
                    if (inputQueue.TryDequeue(out result))
                    {
                        outputQueue.Enqueue(result);
                    }
                });

            Assert.IsTrue(inputQueue.IsEmpty);
            CollectionAssert.AreEquivalent(values.ToArray(), outputQueue.ToArray());
        }
    }
}
